<?php

namespace Admin\Model\DBO;

use Admin\Entity\Book;
use Admin\Entity\Author;
use Zend\Db\TableGateway\AbstractTableGateway;

class CatalogingDocumentDBO extends AbstractTableGateway {

    private $sm;
    private $bookStoreTable;
    private $authorTable;
    private $serialBookTable;

    public function __construct($serviceManager) {
        $this->sm = $serviceManager;
    }

    public function getBookStoreTable() {
        if (!$this->bookStoreTable) {
            $this->bookStoreTable = $this->sm->get('Admin\Model\DAO\BookStoreTable');
        }
        return $this->bookStoreTable;
    }

    public function getAuthorTable() {
        if (!$this->authorTable) {
            $this->authorTable = $this->sm->get('Admin\Model\DAO\AuthorTable');
        }
        return $this->authorTable;
    }

    public function getSerialBookTable() {
        if (!$this->serialBookTable) {
            $this->serialBookTable = $this->sm->get('Admin\Model\DAO\SerialBookTable');
        }
        return $this->serialBookTable;
    }

    public function getLanguage() {
        return $this->getBookStoreTable()->getLanguage();
    }

    public function getBookCategory() {
        return $this->getBookStoreTable()->getBookCategory();
    }

    public function getBookType() {
        return $this->getBookStoreTable()->getBookType();
    }

    public function getBookBarcode($bookBarcode) {
        $bookBarcode = (int) $bookBarcode;
        return $this->getBookStoreTable()->getBookBarcode($bookBarcode);
    }

    public function getInfoBook($bookBarcode) {
        return $this->getBookStoreTable()->getInfoBook($bookBarcode);
    }

    /*
     * Trả về một resulSet
     */

    public function getAllBookFromBarcode($bookBarcode) {
        return $this->getSerialBookTable()->getAllBookFromBarcode($bookBarcode);
    }

    /*
     * Trả về một resulSet
     */

    public function getBookExist($bookBarcode) {
        return $this->getSerialBookTable()->getBookExist($bookBarcode);
    }

    public function saveBook(Book $book, Author $author) {
        // xu ly them mot so nghiep vu de luu vao DB
        $bookBarcode = (int) $book->getBookBarcode();
        $book->setBookBarcode($bookBarcode);
        $yearOfPublish = explode('/', $book->getYearOfPublish());
        $book->setYearOfPublish($yearOfPublish[2] . '-' . $yearOfPublish['0'] . '-' . $yearOfPublish['1']);
        $book->setCreatedTime(date('Y-m-d'));
        $author->setBookBarcode($bookBarcode);
        $arrayAuthor = explode('-', $author->getAuthor());
        // xu ly them ngay gio
        if ($this->getBookStoreTable()->saveBook($book)) {
            foreach ($arrayAuthor as $au) {
                $author->setAuthor(trim($au));
                $this->getAuthorTable()->saveAuthor($author);
            }
            return true;
        }
        else
            return false;
    }

    public function updateBookInfo(Book $book, $bookBarcode) {
        $yearOfPublish = new \DateTime($book->yearOfPublish);
        $data = array(
            'title' => $book->title,
            'numberOfPages' => $book->numberOfPages,
            'publisher' => $book->publisher,
            'yearOfPublish' => $yearOfPublish->format('Y-m_d'),
            'totalNumber' => $book->totalNumber,
            'price' => $book->price,
            'paperSize' => $book->paperSize,
            'location' => $book->location,
            'bookDescription' => $book->bookDescription,
            'usedAs' => $book->usedAs,
            'languageID' => $book->languageID,
            'bookTypeID' => $book->bookTypeID,
            'bookCategoryID' => $book->bookCategoryID,
        );
        $this->getBookStoreTable()-> updateBookInfo($data, $bookBarcode);
    }

}

?>
